//提供默认的路径
//提供文本本地化二次解析

const { ccclass, property } = cc._decorator;

var INVALID_ID = -1; //无效ID;

class AudioUtil {
  curMuiseId: number = -1;
  soundVolume: number = 1.0;
  musicVolume: number = 1.0;
  soundList = [];
  soundEndCBList = {};

  init() {
    this.soundList = [];
    this.soundEndCBList = {};
    this.musicVolume = 1;
    this.soundVolume = 1;
    this.setMusicVolume(this.musicVolume);
    this.setSoundVolume(this.soundVolume);
  }
  playSoundAudioClip(audio: cc.AudioClip, isLoop = false, callBack = null) {
    if (cc.vv.GLSettingDataModel.isSoundOn() == false) {
      console.log("---------sound is off!");
      return INVALID_ID;
    }
    if (audio) {
      this.soundVolume = cc.vv.GLSettingDataModel.getSoundVolume();
      var soundId = cc.audioEngine.play(audio, isLoop, this.soundVolume);
      cc.audioEngine.setFinishCallback(soundId, () => {
        var index = this.soundList.indexOf(soundId);
        if (index != -1) {
          this.soundList.splice(index, 1);
          if (this.soundEndCBList[soundId]) {
            this.soundEndCBList[soundId]();
          }
          delete this.soundEndCBList[soundId];
        }
      });
      this.soundList.push(soundId);
      this.soundEndCBList[soundId] = callBack;
      return soundId;
    }
    return -1;
  }
  //播放音效
  playSound(path, isLoop = false, callBack = null) {
    if (cc.vv.GLSettingDataModel.isSoundOn() == false) {
      console.log("---------sound is off!");
      return INVALID_ID;
    }
    var playSoundfun = res => {
      var soundId = cc.audioEngine.play(res, isLoop, this.soundVolume);
      cc.audioEngine.setFinishCallback(soundId, () => {
        var index = this.soundList.indexOf(soundId);
        if (index != -1) {
          this.soundList.splice(index, 1);
          if (this.soundEndCBList[soundId]) {
            this.soundEndCBList[soundId]();
          }
          delete this.soundEndCBList[soundId];
        }
      });
      this.soundList.push(soundId);
      this.soundEndCBList[soundId] = callBack;
      return soundId;
    };
    var res = cc.loader.getRes(path, cc.AudioClip);
    if (res) {
      return playSoundfun(res);
    }

    cc.loader.loadRes(path, cc.AudioClip, null, (error, res) => {
      if (error) {
        cc.log("load Sound error", path);
        return -1;
      }
      return playSoundfun(res);
    });
  }

  soundIsEnd(soundId) {
    if (typeof soundId == "undefined") {
      return true;
    }
    var isEnd = true;
    if (this.soundList && this.soundList.indexOf(soundId) != -1) {
      isEnd = false;
    }
    return isEnd;
  }

  playMusicAudioClip(audio: cc.AudioClip, isLoop = false) {
    if (cc.vv.GLSettingDataModel.isMusicOn() == false) {
      console.log("---------music is off!");
      return INVALID_ID;
    }
    this.stopMusic();
    this.musicVolume = cc.vv.GLSettingDataModel.getMusicVolume();
    if (audio) {
      return (this.curMuiseId = cc.audioEngine.play(
        audio,
        isLoop,
        this.musicVolume
      ));
    }
    return -1;
  }

  //播放音乐
  playMusic(path: string, isLoop = true) {
    if (cc.vv.GLSettingDataModel.isMusicOn() == false) {
      console.log("---------music is off!");
      return INVALID_ID;
    }
    this.stopMusic();
    this.musicVolume = cc.vv.GLSettingDataModel.getMusicVolume();
    var res = cc.loader.getRes(path, cc.AudioClip);
    if (res) {
      return (this.curMuiseId = cc.audioEngine.play(res, isLoop, this.musicVolume));
    }
    if (!path) path = cc.vv.GLConfig.hallmusicpath[0];
    cc.loader.loadRes(path, cc.AudioClip, null, (error, res) => {
      if (error) {
        cc.log("load Music error", path);
        return -1;
      }
      if (res) {
        return (this.curMuiseId = cc.audioEngine.play(res, isLoop, this.musicVolume));
      }
    });
  }

  //暂停音乐
  pauseMusic() {
    cc.audioEngine.pause(this.curMuiseId);
  }
  //继续音乐
  resumeMusic() {
    cc.audioEngine.resume(this.curMuiseId);
  }
  //停止音乐
  stopMusic() {
    cc.audioEngine.stop(this.curMuiseId);
    this.curMuiseId = -1;
  }

  //停止所有声音
  stopAll() {
    cc.audioEngine.stopAll();
    cc.audioEngine.stopAllEffects();
    this.curMuiseId = -1;
    this.soundList = [];
    this.soundEndCBList = [];
  }

  //暂停所有声音
  pauseAll() {
    cc.audioEngine.pauseAll();
  }

  //恢复所有声音
  resumeAll() {
    cc.audioEngine.resumeAll();
  }
  //获取背景音乐音量
  getMusicVolume() {
    return this.musicVolume;
  }
  //设置背景音乐音量
  setMusicVolume(volume) {
    this.musicVolume = volume;
    cc.audioEngine.setVolume(this.curMuiseId, volume);
  }

  //设置音效音量
  setSoundVolume(volume, soundId = -1) {
    if (soundId === -1) {
      this.soundVolume = volume;
      for (var i = 0; i < this.soundList.length; i++) {
        cc.audioEngine.setVolume(this.soundList[i], volume);
      }
    } else {
      cc.audioEngine.setVolume(soundId, volume);
    }
  }
  //获取音效音量
  getSoundVolume() {
    return this.soundVolume;
  }

  /**
   * 停止音效
   */
  stopSound(audioID) {
    if (this.soundIsEnd(audioID) == false) {
      //音效存在 没有结束
      // 停止音效   监听的是stop事件   stop后recycleAudio 不能再执行到FinishCallback ，这里手动维护列表
      var index = this.soundList.indexOf(audioID);
      if (index != -1) {
        this.soundList.splice(index, 1);
        if (this.soundEndCBList[audioID]) {
          this.soundEndCBList[audioID]();
        }
        delete this.soundEndCBList[audioID];
      }
      return cc.audioEngine.stopEffect(audioID);
    }
  }

  /**
   * 暂停音效
   * @param {*} audioID
   * @returns
   */
  pauseSound(audioID) {
    if (this.soundList && this.soundList.indexOf(audioID) != -1) {
      return cc.audioEngine.pauseEffect(audioID);
    }
  }

  /**
   * 恢复音效
   * @param {*} audioID
   */
  resumeSound(audioID) {
    if (this.soundList && this.soundList.indexOf(audioID) != -1) {
      return cc.audioEngine.resumeEffect(audioID);
    }
  }

  /**
   * 暂停全部音效
   */
  pauseAllSounds() {
    var soundlist = JSON.parse(JSON.stringify(this.soundList));
    soundlist.forEach(element => {
      cc.log("element  is  ", element);
      this.pauseSound(element);
    });
    // cc.audioEngine.pauseAllEffects()//引擎有bug 会暂停音乐
  }

  /**
   * 恢复全部音效
   */
  resumeAllSounds() {
    var soundlist = JSON.parse(JSON.stringify(this.soundList));
    soundlist.forEach(element => {
      cc.log("element  is  ", element);
      this.resumeSound(element);
    });
    //cc.audioEngine.resumeAllEffects()//引擎有bug 会恢复音乐
  }

  /**
   * 停止播放所有音效
   */
  stopAllSounds() {
    var soundlist = JSON.parse(JSON.stringify(this.soundList));
    soundlist.forEach(element => {
      cc.log("element  is  ", element);
      this.stopSound(element);
    });
    // cc.audioEngine.stopAllEffects()//引擎有bug 会停止掉音乐
  }
}

export let audioUtil: AudioUtil = new AudioUtil();
